import request from "@/utils/request";

function getCRUDAPI<T>(basePath: string) {
	async function queryList(
		params: {
			// query
			/** current */
			current?: number;
			/** pageSize */
			pageSize?: number;
		},
		options?: { [key: string]: any },
	) {
		const res = await request(`/${basePath}`, {
			method: 'GET',
			params: {
				...params,
			},
			...(options || {}),
		});
		return {
			success: res.data.records && res.data.records.length >= 0,
			data: res.data.records ?? [],
			total: res.data.total ?? 0
		}
	}
	
	async function add(
		body?: Partial<T>,
		options?: { [key: string]: any },
	) {
		return request(`/${basePath}`, {
			method: 'POST',
			data: body,
			...(options || {}),
		});
	}
	
	async function modify(
		body?: Partial<T>,
		options?: { [key: string]: any },
	) {
		return request(`/${basePath}`, {
			method: 'PUT',
			data: body,
			...(options || {}),
		});
	}
	
	async function remove(
		data: {
			ids: string[]
		},
		options?: { [key: string]: any },
	) {
		return request(`/${basePath}`, {
			method: 'DELETE',
			data: data.ids,
			...(options || {}),
		});
	}
	
	async function removeById(
		id: string,
		options?: { [key: string]: any },
	) {
		return request(`/${basePath}/${id}`, {
			method: 'DELETE',
			...(options || {}),
		});
	}
	return {
		queryList,
		add,
		modify,
		removeById,
		remove
	}
}